/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import { getConInfo, getBranchInfo, userConfig, removeInfo } from '@/services/configuration';

export default {
  namespace: 'configuration',

  state: {
    selectList: [],
    conInfoList: [],
    configurationList: null,
  },

  effects: {
    *getConInfo({ payload, then }, { call, put }) {
      const response = yield call(getConInfo, payload);
      const { brand } = payload;
      const oddDate = [
        {
          brand: 'sto',
          id: 0,
        },
        {
          brand: 'yd',
          id: 3,
        },
        {
          brand: 'yt',
          id: 4,
        },
        {
          brand: 'zt',
          id: 5,
        },
        {
          brand: 'jt',
          id: 6,
        },
        {
          brand: 'ems',
          id: 8,
        },
      ];

      if (!response) return;
      const { msg, code, data } = response;
      if (code == 0) {
        if (data.length == 0 || JSON.stringify(data) == '{}') {
          yield put({
            type: 'save',
            payload: {
              conInfoList: oddDate,
            },
          });
        } else {
          data.forEach(item => {
            for (let i = 0; i < oddDate.length; i += 1) {
              if (oddDate[i].brand == item.brand) {
                oddDate[i] = item;
              }
            }
          });
          yield put({
            type: 'save',
            payload: {
              conInfoList: oddDate,
            },
          });
        }

        then && then(code);
        if (brand) {
          message.success('修改成功');
        }
      } else {
        message.error(msg);
      }
    },
    *getBranchInfo({ payload, then }, { call, put }) {
      const response = yield call(getBranchInfo, payload);
      if (!response) return;
      const { brand } = payload;
      const { code, data } = response;
      if (code == 0) {
        if (!data || data.length == 0) {
          yield put({
            type: 'save',
            payload: {
              selectList: [],
            },
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              selectList: data[brand],
            },
          });
        }

        then(response);
      }
    },
    *userConfig({ payload, then }, { call }) {
      const response = yield call(userConfig, payload);
      if (!response) return;
      const { msg, code } = response;
      if (code == 0) {
        then && then(code);
        message.success('操作成功');
      } else {
        message.error(msg);
      }
    },
    *removeInfo({ payload, then }, { call }) {
      const response = yield call(removeInfo, payload);
      if (!response) return;
      const { msg, code } = response;
      if (code == 0) {
        then && then(code);
        message.success('已删除');
      } else {
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
